
#include <stdio.h>
#include <stdlib.h>

#include <linux/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>





#ifndef CPU_MONITOR_DEBUG
#define CPU_MONITOR_DEBUG 
#endif

#include "cpu_console.h"
#include "../cpu_consts.h"
#include "../cpu_monitor.h"
#include "../cpu_functions.h"

#include "../cpu_parse_display/cpu_display.h"
#include "../cpu_parse_display/cpu_cpuid.h"
/*
 *  cpu_console/cpu_test.c
 *  
 *              CPU Monitor 
 *
 *    Spring Semester.  044800.    Technion.
 *   Copyright (C) 2006  Evgeny Shulman  and Elizabeth Shtrom 
 */
void my_exit( int,int);
int check_im (int fd) ;
int checks(char *drv);
int checks_smp(int cpu_to_process);
int main (void) 
{
	
	int i, cpu,  fd[2];
	char drv[20];
	int cpu_to_process =1;
	
	for ( cpu = 0 ; cpu < cpu_to_process; cpu ++ ) {
		snprintf ( drv ,20, "/dev/cpu_monitor%d", cpu);
		fd[cpu] = open ( drv,O_RDWR);
		if (fd[cpu] < 0){
			fprintf(stderr, "\n%s failed to open, fd=%d\n",drv,fd[cpu]);
			return -1;
		}
		PDEBUG ("\n%s opened, fd=%d\n",drv, fd[cpu]);
	}
	
	
	check_im_smp(fd, 1,5,3,5);
	
	
	for ( cpu = 0 ; cpu < cpu_to_process; cpu ++ ) {
		snprintf ( drv ,20, "/dev/cpu_monitor%d", cpu);
		close ( fd[cpu] ) ; 
		PDEBUG ("\n%s closed, fd=%d\n",drv, fd[cpu]);
	}	
	
	
//	char drv[20]= "/dev/cpu_monitor0";
//	checks(drv);
//	snprintf ( drv ,20, "/dev/cpu_monitor1");
//	checks(drv);
	return 0;
}



void my_exit(int fd , int err ){
	close (fd);
	exit (err);
	
	return;
}



int check_im (int fd) {
	int i;
	PDEBUG("BEFORE");
	check_idt(fd,0,10);
	
	check_im_start(fd);
	PDEBUG("STARTED");
	check_idt(fd,0,10);
	sleep(1);
	
	for ( i = 0 ; i < 2; i ++ ) {
		PDEBUG("READ");
		sleep(1);
		check_im_read (fd);
	}
	check_im_stop (fd);
	PDEBUG("STOPPED");
	check_idt(fd,0,10);
	return 0;
}



int checks(char *drv){

	int fd ;
	fd = open ( drv,O_RDWR);
	if (fd < 0){
		fprintf(stderr, "\n%s failed to open, fd=%d\n",drv,fd);
		return -1;
	}
	 PDEBUG ("\n%s opened, fd=%d\n",drv, fd);

// 	check_idt (fd,0,IDT_ENTRIES);
// 	check_regs(fd);	
// 	check_gdt (fd,0,GDT_ENTRIES);
// 	
// 	check_ldt (fd, 0,LDT_ENTRIES);
// 	check_tss(fd );
// //
//   	check_pgd_and_pt (fd,910,128);
	check_im(fd);
////
// 	check_cpuid(fd);

	close (fd); 
	PDEBUG ("\n %s  closed, fd=%d\n", drv, fd);
	
	return 0;
}



